<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Sort;
use think\Db;
use app\common\model\Question as Ques;

class Question extends Controller
{
    //题库分类
    //分类页面
    public function sort()
    {
        $sort = new Sort;
        if ($this->request->isPost()) {
            $type = $this->request->param('type');
            if ($type == 'listorder') {
                $id = $this->request->param('id/d');
                $value = $this->request->param('value/d');
                $re = Sort::where('Id', $id)->update(['listorder' => $value]);
                if ($re) {
                    return json_x(0, '修改成功');
                } else {
                    return json_x(-1, '修改失败');
                }
            }
            if ($type == 'del') {
                $id = $this->request->param('id/d');
                $re = $sort->del_one($id);
                if ($re === true) {
                    return json_x(0, '删除成功');
                } else {
                    return json_x(-1, $re);
                }
            }
            if ($type == 'delall') {
                $re = $sort->del_all();
                if ($re) {
                    return json_x(0, '清空成功');
                } else {
                    return json_x(-1, '清空失败');
                }
            }
        } else {
            if ($this->request->param('type') == 'get') {
                //获取分组信息
                $sort_list = $sort->api_sortlist();
                return $sort_list;
            }
            return $this->fetch('admin/main/sort');
        }
    }

    public function sort_tree_api()
    {
        $s = new Sort();
        return p_json($s->Get_List_tree());
    }



    //分类添加页
    public function sort_add()
    {
        if ($this->request->isPost()) {
            if(DEMO == 1){
                return json_x(3, '演示模式不允许添加');
            }
            $sort_info = $this->request->post();
            $sort = new Sort;
            if ($sort->add($sort_info)) {
                return json_x(0, '添加成功');
            } else {
                return json_x(-1, '添加失败');
            }
        } else {
            $sort_a = Sort::where('fid', 0)->select();
            $this->assign('sort_list', $sort_a);
            return $this->fetch('admin/main/tpl_sort_add');
        }
    }
    //分类编辑页 - 顶级
    public function sort_edit()
    {
        $sort = new Sort;
        if ($this->request->isPost()) {
            $sort_info = $this->request->post();
            if ($sort->up($sort_info)) {
                return json_x(0, '修改成功');
            } else {
                return json_x(-1, '修改失败');
            }
        } else {
            $id = $this->request->param('id/d');
            $info = Sort::get($id);
            return $this->fetch('admin/main/tpl_sort_edit', ['sort' => $info]);
        }
    }
    //分类编辑页 - 二级
    public function sort_edit_c()
    {
        $sort = new Sort;
        if ($this->request->isPost()) {
            $sort_info = $this->request->post();
            if ($sort->up($sort_info)) {
                return json_x(0, '修改成功');
            } else {
                return json_x(-1, '修改失败');
            }
        } else {
            $id = $this->request->param('id/d');
            $info = Sort::get($id);
            $sort_a = Sort::where('fid', 0)->select();
            $this->assign('sort_list', $sort_a);
            return $this->fetch('admin/main/tpl_sort_edit_c', ['sort' => $info]);
        }
    }
    /**
     * 题库内区块
     */
    //题库列表
    public function index()
    {
        $q = new Ques();
        if ($this->request->isPost()) {
            if(DEMO == 1){
                return json_x(3, '演示模式不允许操作');
            }
            $type = $this->request->param('type');
            if ($type == 'delall') {
                if (Ques::where('Id', '>', 0)->delete()) {
                    return json_x(0, '成功');
                } else {
                    return json_x(-1, '失败');
                }
            }
            if ($type == 'del') {
                $id = $this->request->param('id');
                if (Ques::where('Id', $id)->delete()) {
                    return json_x(0, '成功');
                } else {
                    return json_x(-1, '失败');
                }
            }
        } else {
            if ($this->request->param('type') == 'get') {
                //获取分组信息
                $page = $this->request->param("page") ? $this->request->param("page") : 1;
                $page = intval($page);
                $limit = $this->request->param("limit") ? $this->request->param("limit") : 1;
                $limit = intval($limit);
                $start = $limit * ($page - 1);
                $key = $this->request->param('keyword');
                $q_list = $q->getlist($start, $limit, $key);
                return $q_list;
            }
            return $this->fetch('admin/main/question');
        }
    }
    //修改 试题
    public function edit_question()
    {
        if ($this->request->isPost()) {
            $q = new Ques();
            $data = $this->request->param();
            if ($q->up($data, $data['id'])) {
                return json_x(0, '成功');
            } else {
                return json_x(-1, '失败');
            }
        } else {
            $info = Ques::get($this->request->param('id'));
            return $this->fetch('admin/main/edit_question', ['info' => $info]);
        }
    }
    //添加 试题
    public function add_question()
    {
        if ($this->request->isPost()) {
            $q = new Ques();
            $data = $this->request->param();
            if ($q->add($data)) {
                return json_x(0, '成功');
            } else {
                return json_x(-1, '失败');
            }
        } else {
            //渲染分类
            $sort = Sort::select();
            return $this->fetch('admin/main/add_question', ['sort' => $sort]);
        }
    }
    //批量添加
    public function add_question_s()
    {
        if ($this->request->isPost()) {
            if(DEMO == 1){
                return json_x(3, '演示模式不允许操作');
            }
            $q = new Ques();
            $data = $this->request->param();
            $num = $q->adds($data);
            if ($num != 0) {
                return json_x(0, '添加成功 ' . $num . ' 个试题');
            } else {
                return json_x(-1, '失败');
            }
        } else {
            //渲染分类
            $sort = Sort::select();
            return $this->fetch('admin/main/adds_question', ['sort' => $sort]);
        }
    }
}
